/*
 *
 *
 *
 *
 */

#include "menu.h"

Menu::init(int x, int y){
	xPos = x;
	yPos = y;
	menuItems = new vector<MenuItem*>();
	width = 0;
	height = 0;
}
Menu::Menu(){
	init(0, 0);
}
Menu::Menu(int x, int y){
	init(x, y);
}
void Menu::addMenuItem(MenuIten *item){
	menuItems->push_back(item);
	sizeMenu();
}
void Menu::removeMenuItem(int index){
	if (menuItems->size() > index){
		list<MenuItem>::iterator it = menuItems->begin();
		advance(it, index);
		MenuItem *item = *it;
		menuItems->remove(it);
		sizeMenu();
		delete item;
	}
	
}
void Menu::removeMenuItem(MenuIten *item){
	for (list<MenuItem>::iterator it = menuItems->begin(); it != menuItems->end(); it++){
		if (item == *it){
			menuItems->remove(it);
			sizeMenu();
		}
	}
}
bool Menu::addSubMenu(Menu *submenu, int index){
	if (menuItems->size() > index){
		list<MenuItem>::iterator it = menuItems->begin();
		advance(it, index);
		*it->addSubMenu(submenu);
		return true;
	}
	return false;
}
Menu *Menu::removeSubMenu(int index){
	if (menuItems->size() > index){
		list<MenuItem>::iterator it = menuItems->begin();
		advance(it, index);
		return *it->removeSubMenu(submenu);
	}
	return NULL;
}
void Menu::draw(Display screen){
	screen.guStart();
	screen.drawRectangle(xPos, yPos, width, height);
	int offset = 0;
	for (list<MenuItem>::iterator it = menuItems->begin(); it != menuItems->end(); it++){
		offset += *it->drawMenuItem(screen, xPos, offset);
	}
}
Menu::~Menu(){
	for (vector<MenuItem*>::iterator it = menuItems->begin(); it != menuItems->end(); it++){
		delete *it;
	}
	delete menuItems;
}

/*-------------------------------------------------------------------------------------------------------------*/
MenuItem::MenuItem(char *text, Image *image=NULL){
	this->text = text;
	this->image = image
}
MenuItem::MenuItem(Image *image){
	this->text = NULL;
	this->image = image
}
MenuItem::~MenuItem(){
	if (image) delete image;
	if (text) delete text;
}

/*-------------------------------------------------------------------------------------------------------------*/

//----------------------------
typedef struct{
	char **text;
	colour highlight;
	int selected;
	int start;
	int end;
	int count;
	int action;
	menuFunction selectFunc;
	menuFunction highlightFunc;
	Menu *selectionMenu;
}MenuItem, PMenuItem; 
typedef struct{
	int itemCount;
	char *text;
	MenuItem *items;
	int selected;/* or MenuItem *selected; for linked list */
	int xPos, yPos;
	int indent;
	intraFont* font;
	float fontSize;
	colour textColour;
	int fontStyle;
}Menu, *PMenu;
//---------------------------------
#define OPTIONSMENUTEXT "Options"
#define T4HOURTEXT "24 Hours"
#define ONTEXT "On"
#define OFFTEXT "Off"
#define ALARMSTEXT "Alarms"
#define COLOURTEXT "Colour"
#define BGCOLOURTEXT "bgcolour"
#define BRIGHTNESSTEXT "Brightness"

Menu *createMainMenu(){
	Menu *menu = calloc(1, sizeof(Menu));
	menu->itemCount = 5;
	menu->text = malloc(strlen(OPTIONSMENUTEXT)+1);
	strcpy(menu->text, OPTIONSMENUTEXT);
	menu->textColour = WHITE;
//	menu->selected = 0;
	menu->xPos = 100;
	menu->yPos = 100;
	menu->indent = 50;
	menu->font = intraFontLoad("flash0:/font/ltn4.pgf", 0);
	menu->fontsize = 2.0f;
	MenuItem **menuItemss = calloc(menu->itemCount, sizeof(MenuItem*));
	menu->items = menuItemss;

	menuItems[0] = malloc(sizeof(MenuItem);
	menuItems[0]->text = malloc(sizeof(char*)*3);
	menuItems[0]->text[0] = malloc(strlen(T4HOURTEXT)+1);
	menuItems[0]->highlight = BLUE;
	menuItems[0]->start = 1;
	menuItems[0]->end = 2;
	menuItems[0]->highlight = setConfig24Hour;
	strcpy(menuItems[0]->text[0], T4HOURTEXT);
	menuItems[0]->text[1] = malloc(strlen(ONTEXT)+1);
	strcpy(menuItems[0]->text[1], ONTEXT);
	menuItems[0]->text[2] = malloc(strlen(OFFTEXT)+1);
	strcpy(menuItems[0]->text[2], OFFTEXT);

	menuItems[1] = malloc(sizeof(MenuItem);
	menuItems[1]->text = malloc(sizeof(char*)*1);
	menuItems[1]->text[0] = malloc(strlen(ALARMSTEXT)+1);
	menuItems[1]->highlight = BLUE;
//	menuItems[1]->start = 0;
//	menuItems[1]->end = 0;
	strcpy(menuItems[1]->text[0], ALARMSTEXT);

	menuItems[2] = malloc(sizeof(MenuItem);
	menuItems[2]->text = malloc(sizeof(char*)*1);
	menuItems[2]->text[0] = malloc(strlen(COLOURTEXT)+1);
	menuItems[2]->highlight = BLUE;
//	menuItems[2]->start = 0;
//	menuItems[2]->end = 0;
	strcpy(menuItems[2]->text[0], COLOURTEXT);

	menuItems[3] = malloc(sizeof(MenuItem);
	menuItems[3]->text = malloc(sizeof(char*)*1);
	menuItems[3]->text[0] = malloc(strlen(BGCOLOURTEXT)+1);
	menuItems[3]->highlight = BLUE;
//	menuItems[3]->start = 0;
//	menuItems[3]->end = 0;
	strcpy(menuItems[3]->text[0], BGCOLOURTEXT);

	menuItems[4] = malloc(sizeof(MenuItem);
	menuItems[4]->text = malloc(sizeof(char*)*1);
	menuItems[4]->text[0] = malloc(strlen(BRIGHTNESSTEXT)+1);
	menuItems[4]->highlight = BLUE;
//	menuItems[4]->start = 0;
//	menuItems[4]->end = 0;
	strcpy(menuItems[4]->text[0], BRIGHTNESSTEXT);
	
	return menu;
}
void freeMenu(Menu *menu){
	int i, j;
	for (i = 0; i < menu->count; i++){
		for (j = 0; j < menu->items[i]->count; j++){
			free(menu->items[i]->text[j]);
		}
		free(menu->items[i]->text);
		free(menu->items[i]);
	}
	free(menu->items);
	free(menu);
}
